package com.test.searchview;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class frag2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = getFragmentManager();

        if (fm.findFragmentById(android.R.id.content) == null) {
            Fragment list = new Fragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        }
        
    }
    
    public static class ItemLoader extends AsyncTaskLoader<ArrayList<String>> {
        
    	private ArrayList<String> ArrayItems;
    	
        public ItemLoader(Context context) {
			super(context);
		}

        @Override
        public ArrayList<String> loadInBackground() {
        	ArrayItems = new ArrayList<String>();
        		ArrayItems.add("Tie");
        		ArrayItems.add("Ler");
        		ArrayItems.add("Jen");
            return ArrayItems;
        }
        
        @Override
        public void deliverResult(ArrayList<String> apps) {
            if (isStarted()) {
                super.deliverResult(apps);
            }
        }
        
        @Override protected void onStartLoading() {
        	forceLoad();
        }
    }

    public static class Fragment extends ListFragment
            implements OnQueryTextListener, LoaderManager.LoaderCallbacks<ArrayList<String>> {

    	private ArrayAdapter<String> mAdapter;

        String mCurFilter;

        @Override public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);            
            mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
            setListAdapter(mAdapter);
            setHasOptionsMenu(true);
            setListShown(false);
            getLoaderManager().initLoader(0, null, this);
        }
        
        @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.layout.menu_item_frag2, menu);
            SearchView sv = (SearchView) menu.findItem(R.id.menu_search_frag2).getActionView();
            sv.setOnQueryTextListener(this);
        }
        
        @Override public boolean onQueryTextChange(String newText) {
        	mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
            mAdapter.getFilter().filter(mCurFilter);
            return true;
        }
        
        @Override public boolean onQueryTextSubmit(String query) {
            return true;
        }
        
        @Override public void onListItemClick(ListView l, View v, int position, long id) {
            Log.i("FragmentComplexList", "Item clicked: " + id);
        }
        
        public Loader<ArrayList<String>> onCreateLoader(int id, Bundle args) {        	       	
    		return new ItemLoader(getActivity());
        }

        public void onLoadFinished(Loader<ArrayList<String>> loader, ArrayList<String> data) {
            mAdapter.clear();
            mAdapter.addAll(data);
            
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
        }

        public void onLoaderReset(Loader<ArrayList<String>> loader) {
        	
        }
    }
}